package com.patMall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.patMall.entity.Concern;
import com.patMall.entity.User;
import com.patMall.service.ConcernService;
import com.patMall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 75099637@qq.com
 * @PackageName myMovie
 * @Package com.movie.controller
 * @Date 2023/12/14 8:53
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private ConcernService concernService;

    @PostMapping(value = "/login")
    @ResponseBody
    public Object UserLogin(User user, HttpSession session) {
        Map map = new HashMap();
        List<User> users = userService.getUser(user.getUsername());
        if (users != null && users.size() > 0) {
            User loginUser = users.get(0);
            List<Concern> concernList = concernService.getConcern(loginUser.getUserId());
            loginUser.setConcernList(concernList);
            if (user.getUserPwd().equals(loginUser.getUserPwd())) {
                session.setAttribute("userJson", loginUser);
                map.put("msg", "success");
                map.put("data", loginUser);
                return map;
            } else {
                return map.put("msg", "error");
            }
        }
        return map.put("msg", "fail");
    }
    @PostMapping(value = "/adminLogin")
    @ResponseBody
    public Object adminLogin(User user, HttpSession session) {
        Map map = new HashMap();
        User user1 = userService.selectUsername(user.getUsername());
        if (user1 != null && user1.getUserRole() > 0) {
            User loginUser = user1;
            List<Concern> concernList = concernService.getConcern(loginUser.getUserId());
            loginUser.setConcernList(concernList);
            if (user.getUserPwd().equals(loginUser.getUserPwd())) {
                session.setAttribute("adminJson", loginUser);
                map.put("msg", "success");
                map.put("data", loginUser);
                return map;
            } else {
                return map.put("msg", "error");
            }
        }
        return map.put("msg", "noPower");
    }

    @PostMapping("/register")
    @ResponseBody
    public String UserRegister(User user) {
        List<User> users = userService.selectByUserNameUser(null,user.getUsername(),0);
        if (users != null && users.size() > 0) {
            return "existed";
        } else {
            user.setNickName(user.getUsername()+"_"+System.currentTimeMillis());
            int row = userService.addUser(user);
            if (row == 1) {
                return "success";
            }else{
                return "fail";
            }
        }
    }
    @RequestMapping(value = "/modifyUserPwd")
    @ResponseBody
    public String UserUpdatePwd(String oldPwd,String newPwd,Integer userId) {
        if (oldPwd != null || newPwd != null || userId != null) {
            if (userService.getUserId(userId).getUserPwd().equals(oldPwd)){
                User user = new User(userId,newPwd);
                int count = userService.updateUser(user);
                return "success";
            }
        }
            return "fail";
    }
    @RequestMapping("/userRegisterCode")
    @ResponseBody
    public String UserRegisterCode(){
return "success";
    }

    @ApiOperation(value = "注销用户",notes = "注销",httpMethod = "Get")
    @GetMapping( "/exit")
    @ResponseBody
    public String getLogout(HttpSession session){
        session.removeAttribute("userJson");
        session.removeAttribute("adminJson");
        return "success";
    }
    @PostMapping("getUser")
    @ResponseBody
    public Object getUser(Integer userId){
        Map map = new HashMap();
        User user = userService.getUserId(userId);
        if (user != null){
            map.put("msg","success");
            map.put("user",user);
        }else {
            map.put("msg","fail");
        }
        return map;
    }
    @PostMapping("updateName")
    @ResponseBody
    public String updateName(User user){
        int count = userService.updateUser(user);
        if (count == 1) {
            return "success";
        }else{
            return "fail";
        }
    }
    @PostMapping("addIdCard")
    @ResponseBody
    public String addIdCard(User user){
        List<User> userIdCard = userService.getAllByIdCard(user.getIdCard());
        if (userIdCard.size() > 0){
            return "existed";
        }else {
            int count = userService.updateUser(user);
            if (count == 1){
                return "success";
            }else {
                return "fail";
            }
        }
    }
    @PostMapping("addPhone")
    @ResponseBody
    public String addPhone(User user){
        List<User> userPhone = userService.getAllByPhone(user.getPhone());
        if (userPhone.size() > 0){
            return "existed";
        }else {
            int count = userService.updateUser(user);
            if (count == 1){
                return "success";
            }else {
                return "fail";
            }
        }
    }

    @PostMapping("userCancel")
    @ResponseBody
    public String userCancel(User user){
            user.setUserRole(1);
            int count = userService.updateUser(user);
            if (count == 1){
                return "success";
            }else {
                return "fail";
            }
    }

    @PostMapping("/findAllUser")
    @ResponseBody
    public Object findAllUser(int page,int limit,String role) {
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<User> users = userService.findAllUserRole(role,0);
        Page<User> pageList =(Page<User>) users;
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }

    @PostMapping("/findUserInfosByName")
    @ResponseBody
    public Object findUserInfosByName(int page,int limit,String role,String username){
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<User> userList = userService.selectByUserNameUser(role,username,0);
        System.err.println(userList);
        Page<User> pageList =(Page<User>) userList;
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }

    @PostMapping("/findAllUserByAdmin")
    @ResponseBody
    public Object findAllUserByAdmin(int page,int limit,String role) {
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<User> users = userService.findAllUserRole(role,1);
        Page<User> pageList =(Page<User>) users;
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }

    @PostMapping("/findUserInfosByAdminName")
    @ResponseBody
    public Object findUserInfosByAdminName(int page,int limit,String role,String username){
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<User> userList = userService.selectByUserNameUser(role,username,1);
        System.err.println(userList);
        Page<User> pageList =(Page<User>) userList;
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }

}
